# -*- coding: UTF-8 -*-

from .BaseService import BaseService

class WechatShareInfoStatisticService(BaseService):
    
    def sumMiniprogramData(self, gmtTime = 0, limit = 10):
        gmtTime = int(gmtTime)

        queryList, whereList = [], [];
        queryList.append('max(`title`) as title')
        queryList.append('appname')
        queryList.append('count(*) as count')
        queryList.append('max(`target_url`) as target_url')
        whereList.append('source_type = 2')
        whereList.append('appname != ""')
        if gmtTime > 0:
            whereList.append('gmt_create >= %d' % gmtTime)

        wechatGroupShareDataModel = self.core.loader.model('WechatGroupShareDataModel')
        return wechatGroupShareDataModel.listByWhere(','.join(queryList), ' AND '.join(whereList), 'appname', 'count DESC', str(limit))


    def sumH5Data(self, gmtTime = 0, limit = 10):
        gmtTime = int(gmtTime)

        queryList, whereList = [], [];
        queryList.append('substr(target_url, 1, if(instr(`target_url`, "?") > 0, instr(`target_url`, "?") - 1, length(`target_url`))) as suburl')
        queryList.append('max(`title`) as title')
        queryList.append('max(`target_url`) as target_url')
        queryList.append('count(*) as count')
        whereList.append('source_type = 1')
        whereList.append('target_url != ""')
        if gmtTime > 0:
            whereList.append('gmt_create >= %d' % gmtTime)

        wechatGroupShareDataModel = self.core.loader.model('WechatGroupShareDataModel')
        return wechatGroupShareDataModel.listByWhere(','.join(queryList), ' AND '.join(whereList), 'suburl', 'count DESC', str(limit))
